<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='文章管理',active='article'" th:fragment="userList">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="deleteConfirm" th:content='确认删除该动态'/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
</head>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">用户管理</h4>
                    </div>
                    <div class="col-md-12">
                        <ol>
                            <table class="table table-striped table-bordered">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>标题</th>
                                    <th>标签</th>
                                    <th>发布时间</th>
                                    <th>作者</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <th:block th:each="article : ${articleList.list}">
                                    <tr th:id="${article.id}">
                                        <td>
                                            <th:block th:text="${article.id}"/>
                                        </td>
                                        <td>
                                            <th:block th:text="${article.title}"/>
                                        </td>
                                        <td>
                                            <th:block th:text="${article.tags}"/>
                                        </td>
                                        <td>
                                            <th:block th:text="${commons.dateFormat(article.created)}"/>
                                        </td>

                                        <td>
                                            <div th:each="user:${userList.list}">
                                                <th:block th:if="${article.user_id==user.id}"
                                                          th:text="${user.username}"/>
                                            </div>
                                        </td>

                                        <td>
                                            <a th:if="${article.valid==0}" href="javascript:void(0)"
                                               th:onclick="empowerArticle([[${article.id}]])"
                                               id="type" class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                                <i class="fa fa-edit"></i> <span>授权</span></a>
                                            <a th:unless="${article.valid==0}" href="javascript:void(0)"
                                               th:onclick="deleteArticle([[${article.id}]])"
                                               class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                                <i class="fa fa-trash-o"></i> <span>禁用</span></a>
                                        </td>
                                    </tr>
                                </th:block>
                                </tbody>
                            </table>
                        </ol>
                        <!-- 进行评论分页 -->
                        <div class="lists-navigator clearfix">
                            <ol class="page-navigator">
                                <!-- 判断并展示上一页 -->
                                <th:block th:if="${articleList.hasPreviousPage}">
                                    <td class="prev"><a th:href="'?apage='+${articleList.prePage}+'#userList'">上一页</a>
                                    </td>
                                </th:block>
                                <!-- 判断并展示中间页 -->
                                <th:block th:each="navIndex : ${articleList.navigatepageNums}">
                                    <th:block th:if="${articleList.pages} <= 5">
                                        <td th:class="${articleList.pageNum}==${navIndex}?'current':''">
                                            <a th:href="'?apage='+${navIndex}+'#userList'" th:text="${navIndex}"></a>
                                        </td>
                                    </th:block>
                                    <th:block th:if="${articleList.pages} > 5">
                                        <td th:if="${articleList.pageNum <=3 &&  navIndex <= 5}"
                                            th:class="${articleList.pageNum}==${navIndex}?'current':''">
                                            <a th:href="'?apage='+${navIndex}+'#userList'" th:text="${navIndex}"></a>
                                        </td>
                                        <td th:if="${articleList.pageNum >= articleList.pages-2 &&  navIndex > articleList.pages-5}"
                                            th:class="${articleList.pageNum}==${navIndex}?'current':''">
                                            <a th:href="'?apage='+${navIndex}+'#userList'" th:text="${navIndex}"></a>
                                        </td>
                                        <td th:if="${articleList.pageNum >=4 && articleList.pageNum <= articleList.pages-3 &&  navIndex >= articleList.pageNum-2 && navIndex <= articleList.pageNum+2}"
                                            th:class="${articleList.pageNum}==${navIndex}?'current':''">
                                            <a th:href="'?apage='+${navIndex}+'#userList'" th:text="${navIndex}"></a>
                                        </td>
                                    </th:block>
                                </th:block>
                                <!-- 判断并展示下一页 -->
                                <th:block th:if="${articleList.hasNextPage}">
                                    <td class="next"><a th:href="'?apage='+${articleList.nextPage}+'#userList'">下一页</a>
                                    </td>
                                </th:block>
                            </ol>
                        </div>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script type="text/javascript">
    function deleteArticle(id) {
        // 获取<meta>标签中封装的_csrf信息
        var token = $("meta[name='deleteConfirm']").attr("content");
        var header = $("meta[name='deleteConfirm']").attr("content");

        if (confirm('确定禁用该动态吗?')) {

            $.post(
                '/admin/deleteArticleById',
                {id: id},

                function (data) {
                    if (data == "OK") {
                        window.alert("动态禁用成功");
                    } else {
                        window.alert("动态禁用成功")
                    }
                    window.location.reload();
                }
            );
        }
    }

    function empowerArticle(id) {

        if (confirm('确定授权该动态吗?')) {

            $.post(
                '/admin/empowerArticleById',
                {id: id},

                function (data) {
                    if (data == "OK") {
                        window.alert("动态授权成功");
                    } else {
                        window.alert("动态授权成功")
                    }
                    window.location.reload();
                }
            );
        }
    }
</script>
</body>
</html>